<?php
$page_title = 'eTAT: Add Group';
$style = 'addgroup';
$script = 'addgroup';
require_once 'controller.php';
if(isset($params->x)) {
	$body = '';
	for($c=1; $c<=$params->x; $c++) {
		$body .= "<div id='student$c'><span>$c. </span>";
		$body .= newText('full_name'.$c,'Full Name',array('class'=>'visible'));
		$body .= newText('gmail'.$c,'Gmail Address',array('class'=>'visible'));
		$body .= "</div>";
	}
	die($body);
}
if(isset($params->c)) {
	$course = $instructor->getCourse($params->c);
} else {
	redirect('myetat.php');
}
if(isset($_POST['add_group_submit'])) {
	$data->group_name = $_POST['group_name'];
	$data->group_description = $_POST['group_description'];
	$data->google_code = $_POST['google_code'];
	$data->google_groups = $_POST['google_groups'];
	if(isset($_POST['student_count'])) {
		$count = $_POST['student_count'];
		for($i=1; $i<=$count; $i++) {
			$full_name = $_POST["full_name$i"];
			$gmail = $_POST["gmail$i"];
			$group_name = $_SESSION['add_project']->group_name;
			if($full_name != '' || $gmail != '')
			$students[] = (object) array('full_name'=>$full_name,'gmail'=>$gmail,'group'=>$group_name,'course'=>$course->id);
		}
	}
	$data->students = $students;
	$course->addGroup($data);
	redirect("course.php?c=$course->id");
}
$courses = $instructor->getAllCourses();
require_once 'header.php';
include_once('leftmenu.php');
?>
<div id="main">
	<h1><?php print $course->title; ?>: Add Group</h1>
	<?php
		$form = new Form('final_form','post');
		$form->textInput('group_name','Group Name',array('required'=>true));
		$form->textArea('group_description','Group Description',3,25);
		$form->textInput('google_code','Google Code URL',array('required'=>true),false);
		$form->button('check_google_code','Check',array('type'=>'button','onclick'=>'checkGoogleCode();'),false);
		$form->text("<div class='clear'></div>");
		$form->textInput('google_groups','Google Groups URL',array('required'=>true),false);
		$form->button('check_google_groups','Check',array('type'=>'button','onclick'=>'checkGoogleGroups();'),false);
		$form->text("<div class='clear'></div>");
		$form->textInput('student_count','Enter number of students',array('size'=>'1'),false);
		$form->button('student_count_submit','Add',array('type'=>'button'),false);
		$form->text("<div id='students'></div>");
		$form->button('add_group_submit','Add this group',array(),false);
		$form->button('add_group_cancel','Cancel',array('type'=>'button','onclick'=>"redirect('course.php?c=$course->id')"),false);
		print $form->build();
		$check_confirm = newBtn('check_confirm','Yes, this is correct',array('type'=>'button','onclick'=>'closePopup();'),false);
		$check_cancel = newBtn('check_cancel','No, search again',array('type'=>'button','onclick'=>'closePopup();'),false);
		$check_body = "
			 <div id='check-output'></div>
			 <div id='row-check-confirm' style='padding:10px;'>
			 $check_confirm $check_cancel
			 </div>";
			 print popup('check-frame',$check_body,900,array('top'=>90,'left'=>40,'padding'=>0));
	?>
</div>
<?php require_once 'footer.php'; ?>